package org.soenea.doitall.domain.model.invite.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;

import org.dsrg.soenea.domain.DomainObjectCreationException;
import org.dsrg.soenea.domain.MapperException;
import org.dsrg.soenea.domain.mapper.DomainObjectNotFoundException;
import org.dsrg.soenea.domain.user.UserProxy;
import org.soenea.doitall.domain.model.group.GroupProxy;
import org.soenea.doitall.domain.model.group.IGroup;
import org.soenea.doitall.domain.model.invite.IInvite;
import org.soenea.doitall.domain.model.invite.Invite;
import org.soenea.doitall.domain.model.person.IPerson;
import org.soenea.doitall.domain.model.person.Person;
import org.soenea.doitall.domain.model.person.PersonProxy;
import org.soenea.doitall.domain.model.person.data.PersonFinder;
import org.soenea.doitall.domain.model.person.data.PersonTDG;
import org.soenea.doitall.domain.model.invite.InviteStatus;
import org.soenea.doitall.domain.model.invite.data.InviteFinder;



public class InviteInputMapper {
	public static List<Invite> findAll() throws MapperException {
		List<Invite> groups = new Vector<Invite>();
		try {
			ResultSet rs = InviteFinder.findAll();
			while (rs.next()) {
				groups.add(getInvite(rs));
			}
		} catch (SQLException e) {
			throw new MapperException(e);
		}
		return groups;
	}

	public static Invite find(long id) throws MapperException,
			DomainObjectNotFoundException {
		try {
			ResultSet rs = InviteFinder.find(id);
			if (rs.next()) {
				return getInvite(rs);
			} else {
				throw new DomainObjectNotFoundException("Invite " + id);
			}
		} catch (SQLException e) {
			throw new MapperException(e);
		}
	}

	private static Invite getInvite(ResultSet rs) throws SQLException {
		Invite i = new Invite(rs.getLong("i.inviteID"), rs.getLong("i.version"), new PersonProxy(rs.getLong("i.userIdInviter")), new PersonProxy(rs.getLong("i.userIdInvited")), new GroupProxy(rs.getLong("i.groupNum")),InviteStatus.values()[rs.getInt("i.status")]);
		return i;
	}
	public static List<IInvite> find(IGroup group) throws SQLException, MapperException,DomainObjectCreationException{
		ResultSet rs = InviteFinder.findByGroup(group.getId());
		List<IInvite> groups = new Vector<IInvite>();
		while (rs.next()) {
			groups.add(getInvite(rs));
		}
		return groups;
	}
}
